﻿using SDF.Admin.Extensions;
using SDF.Admin.Infrastructure.Mapper;
using SDF.Core.Infrastructure;
using SDF.Plugin.Misc.SAPAdapter.Domain;
using SDF.Plugin.Misc.SAPAdapter.Models;
using SDF.Services.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SDF.Core;

namespace SDF.Plugin.Misc.SAPAdapter.Extensions
{
    public static class MappingExtensions
    {
        private static readonly IDateTimeHelper _dateTimeHelper = EngineContext.Current.Resolve<IDateTimeHelper>();

        #region common
        public static TDestination MapTo<TSource, TDestination>(this TSource source)
        {
            return AutoMapperConfiguration.Mapper.Map<TSource, TDestination>(source);
        }

        public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
        {
            return AutoMapperConfiguration.Mapper.Map(source, destination);
        }
        #endregion

        public static RfcReadTableParamsModel ToRfcReadTableParamsModel(this TableDataAPIModel value)
        {
            return new RfcReadTableParamsModel()
            {
                RowCount = value.RowCount,
                RowSkip = value.RowSkip,
                WhereClause = value.WhereClause,
                Fields = value.Fields
            };
        }

        public static SAPClientModel ToModel(this SAPInstance entity)
        {
            var model = new SAPClientModel()
            {
                ApplicationServer = entity.ApplicationServer,
                CodeName = entity.CodeName,
                CreateTime = entity.CreateTimeOnUtc,
                Delete = entity.Delete,
                Id = entity.Id,
                Language = entity.Language,
                LogonGroup = entity.LogonGroup,
                MessageServerHost = entity.MessageServerHost,
                Name = entity.Name,
                Password = entity.Password,
                SAPRouter = entity.SAPRouter,
                SystemID = entity.SystemID,
                SystemNumber = entity.SystemNumber,
                User = entity.User,
                Client = entity.Client
            };

            model.CreateTime = _dateTimeHelper.ConvertToUserTime(model.CreateTime, DateTimeKind.Utc);

            return model;
        }

        public static SAPInstance ToEntity(this SAPClientModel model, SAPInstance entity)
        {
            entity.ApplicationServer = model.ApplicationServer;
            entity.CodeName = model.CodeName;
            entity.Language = model.Language;
            entity.LogonGroup = model.LogonGroup;
            entity.MessageServerHost = model.MessageServerHost;
            entity.Name = model.Name;
            entity.Password = model.Password;
            entity.SAPRouter = model.SAPRouter;
            entity.SystemID = model.SystemID;
            entity.SystemNumber = model.SystemNumber;
            entity.User = model.User;
            entity.Client = model.Client;
            entity.CreateTimeOnUtc = entity.CreateTimeOnUtc.IsMinValue() ? DateTime.Now : model.CreateTime;

            entity.CreateTimeOnUtc = _dateTimeHelper.ConvertToUtcTime(entity.CreateTimeOnUtc, DateTimeKind.Local);
            return entity;
        }

        public static QuerySAPInstanceModel ToQueryModel(this SAPInstance client)
        {
            return new QuerySAPInstanceModel()
            {
                SAPInstance = client.CodeName,
                Name = client.Name
            };
        }
    }
}